package com.supergo.zuul.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Classname CorsConfig
 * @Description 解决跨域问题过滤器
 * @Date 2020/10/26 16:06
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter(){
        // 添加Cors跨域配置信息
        CorsConfiguration config = new CorsConfiguration();
        // 允许跨域访问的客户端域
        config.addAllowedOrigin("*");
        // 允许请求带有验证信息
        config.setAllowCredentials(true);
        // 允许客户端跨域访问可以携带请求头
        config.addAllowedHeader("*");
        // 允许客户端跨域访问请求方式
        config.addAllowedMethod("*");
        // 添加映射
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", config);
        CorsFilter corsFilter = new CorsFilter(configurationSource);
        return corsFilter;
    }
}
